/*
* Copyright (c) 2009, Active Web Solutions Limited
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1- Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2- Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3- The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
*
* THIS SOFTWARE IS PROVIDED BY Active Web Solutions Limited ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL Active Web Solutions Limited BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
package net.aws.jazure;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import net.aws.jazure.util.ISO8601Helper;

/**
 * This class encapsulates a single property in an entity
 * in a table in the table storage service.
 * 
 * Each property has a value and a type. 
 * @author dyaa.albakour
 *
 */
public class TableStorageProperty {
	
	// The available ADO.NET DataServices types
	// as per http://msdn.microsoft.com/en-us/library/dd179338.aspx	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmBinary = "Edm.Binary";	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmBoolean = "Edm.Boolean";	
	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmDateTime = "Edm.DateTime";
	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmDouble = "Edm.Double";
	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmInt32 = "Edm.Int32";
	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmInt64 = "Edm.Int64";
	
	/**
	 * An ADO.NET DataServices type.
	 */
	public static final String EdmString = "Edm.String";
	
	
	
	public static final String PartitionKey = "PartitionKey";
	public static final String RowKey  = "RowKey";
	public static final  String Timestamp = "Timestamp";
	
	/**
	 * String Representation of the data type,
	 * At the moment, this is used only as a label.
	 * In the future we need to create a 'PropertyType' class that can
	 * determine the XML representation of the property.
	 * 
	 */
	protected String type; 
//	protected String name;
	protected Object value;
	
	public TableStorageProperty(String type, Object value) {
		
		super();
		this.type = type;
		this.value = value;
	}
	public String getType() {
		return type;
	}
	public Object getValue() {
		return value;	
	}	
	public TableStorageProperty() {
		super();
	}
	
	/**
	 * Marshalls the value of the property.
	 * @return A string representing the value.
	 */
	public String marshallValue(){
		
		if(value instanceof String){
			//TODO UTF 16 encoding
			String str = (String) value;
			return str;
		} else if(value instanceof Integer){
			return ((Integer)value).intValue()+"";
		} else if (value instanceof Calendar){
			
			// ** That does not work,
			//	The Datetime should be formatted in ISO8061, as per
			//		http://msdn.microsoft.com/en-us/library/dd894033.aspx
			// 		http://www.w3.org/TR/NOTE-datetime
			/*
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
			Calendar calendarValue = (Calendar)value;
			String formattedTimestamp =  fmt.format(calendarValue.getTime());
			return formattedTimestamp;
			*/
			Calendar calendarValue = (Calendar)value;
			return ISO8601Helper.formatISO8061(calendarValue.getTime());
			
		}      
		
		// TODO other data types.
		return null;
	}
	
	/**
	 * Unmarshalls the string representation of the value and
	 * store it.
	 * @param type the type of the property.
	 * @param value the string representation of its value.
	 */
	public void unmrshallValue(String type, String value) {
	
		// TODO this really a rubbish way of marshalling/unmarshalling
		// Need to think of smarter ways of reflecting on the object type
		// or probably add methods like FromXMLNode, toXMLNode
		if(type ==null)
			type = new String(EdmString);		
		this.type = type;
		if(type.equals(EdmString)){
			this.value = value;
		}else if(type.equals(EdmInt32)){
			this.value = new Integer(value);			
		}else if (type.equals(EdmDateTime)){
			/*
			SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSSZ");
			Calendar calendarValue=  Calendar.getInstance();
			calendarValue.setTime(fmt.parse(value));
			this.value = calendarValue;*/
			Calendar calendarValue=  Calendar.getInstance();
			calendarValue.setTime(ISO8601Helper.parseISO8061(value));
		}
		//TODO other data types
		
	}
	
	
	

	public static void main(String[] args) throws UnsupportedEncodingException, ParseException{
		
		String arbitraryString = "This string is awesome !! Ins't It?";
		
		byte[] arbitraryStringUTF16bytes= arbitraryString.getBytes("UTF-16");		
		
		String strUTF16 = new String(arbitraryStringUTF16bytes,"UTF-16");
		
		System.out.println(strUTF16);
		
		
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSSSSZ");
		Calendar calendarValue=  Calendar.getInstance();		
//		System.out.println(fmt.format(calendarValue.getTime()));		
//		calendarValue.setTime(fmt.parse("2009-07-21T17:52:54.6854391Z"));
		
		
		System.out.println( ISO8601Helper.formatISO8061(calendarValue.getTime()) );	
		
		calendarValue.setTime( ISO8601Helper.parseISO8061("2009-07-21T17:52:54.6854391Z") );
		System.out.println(fmt.format(calendarValue.getTime()) );
		
	}
	
	
	
}

